//
//  CXPresentationManager.swift
//  CXSwiftWB
//
//  Created by meidaeriOS on 2017/5/22.
//  Copyright © 2017年 meidaer. All rights reserved.
//

import UIKit

/// 自定转场展现
let XMGPresentationManagerDidPresented = "XMGPresentationManagerDidPresented"
/// 自定义转场消失
let XMGPresentationManagerDidDismissed = "XMGPresentationManagerDismissed"

class CXPresentationManager: NSObject, UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning {
    
    fileprivate var isPresent = false
    
    var presentFrame = CGRect.zero
    
    // MARK: - UIViewControllerTransitioningDelegate

    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController?
    {
        let pc = CXPresentationController(presentedViewController: presented, presenting: presenting)
        pc.presentFrame = presentFrame
        return pc
    }
    
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresent = true
        NotificationCenter.default.post(name: Notification.Name.init(rawValue: XMGPresentationManagerDidPresented), object: self)
        return self;
    }
    
    /// 消失的协议
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        NotificationCenter.default.post(name: Notification.Name.init(rawValue: XMGPresentationManagerDidDismissed), object: self)
        return self
    }
    
    
    
    // MARK: - UIViewControllerAnimatedTransitioning
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval
    {
        return 0.5
    }
    
    // This method can only  be a nop if the transition is interactive and not a percentDriven interactive transition.
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning)
    {
        if isPresent {
            // 展现
            willPresentedController(transitionContext)
        } else {
            // 消失
            willDismissedController(transitionContext)
        }
    }
    
    // 执行展现动画
    private func willPresentedController(_ transitionContext: UIViewControllerContextTransitioning)
    {
        guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
            return
        }
        
        transitionContext.containerView.addSubview(toView)
        
        // 执行动画
        toView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0)
        // 设置锚点
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { 
            toView.transform = CGAffineTransform.identity
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }
    
    fileprivate func willDismissedController(_ transitionContext: UIViewControllerContextTransitioning) {
        guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
            return
        }
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: { 
            fromView.transform = CGAffineTransform(scaleX: 1.0, y: 0.0000001)
        }) { (_) in
            transitionContext.completeTransition(true)
        }
    }

}
